home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / asm / RemoveWS1_0.lha / RemoveWS / RemoveWS.c next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  1.5 KB  |  110 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int lineread(char*,FILE*);
  6. int linewrite(char*,FILE*);
  7. int lineconvert(char*,char*);
  8.  
  9. main (int argc, char* argv[]){
  10. char c;
  11. char s[255],t[255];
  12. int i,j;
  13. FILE *fp1,*fp2;
  14.  
  15.     if (argc>=2){
  16.         if (strcmp(argv[1],"?") == 0){
  17.             printf("USAGE: %s InFile OutFile\n",argv[0]);
  18.             exit(0);
  19.         }
  20.     }
  21.  
  22.     if (argc>=2){
  23.         if ((fp1 = fopen(argv[1],"r")) == NULL){
  24.             fputs("can`t open infile\n",stderr);
  25.             exit(5);
  26.         }
  27.     }
  28.     else{
  29.     /*    fputs("use stdin\n",stderr);*/
  30.         fp1=stdin;
  31.     }
  32.  
  33.     if (argc>=3){
  34.         if ((fp2 = fopen(argv[2],"w")) == NULL){
  35.             fputs("can`t open outfile\n",stderr);
  36.             fclose(fp1);
  37.             exit(5);
  38.         }
  39.     }
  40.     else{
  41.     /*    fputs("use stdout\n",stderr);*/
  42.         fp2=stdout;
  43.     }
  44.  
  45.     while (lineread(s,fp1) == 0){
  46.     lineconvert(s,t);
  47.     linewrite(t,fp2);
  48.     }
  49.  
  50.     fclose(fp1);
  51.     fclose(fp2);
  52.  
  53. exit(0);
  54. }
  55.  
  56. lineread(char s[255],FILE *fp1){
  57. int i=0;
  58. char c;
  59.     while ((c = getc(fp1)) != EOF){
  60.         if (c == 0x0a){
  61.             s[i]=0;
  62.             return(0);
  63.         }
  64.         else{
  65.             if (i<255){
  66.                 s[i++]=c;
  67.             }
  68.             else{
  69.                 s[i]=0;
  70.                 return(0);
  71.             }
  72.         }
  73.     }
  74.     return(1);
  75. }
  76.  
  77. linewrite(char t[255],FILE *fp2){
  78. int i=0;
  79.  
  80.     if (t[0] != 0){
  81.         while (t[i] != 0) putc(t[i++],fp2);
  82.         putc(0x0a,fp2);
  83.     }
  84. }
  85.  
  86. lineconvert(char s[255],char t[255]){
  87. int i=0,j=0;
  88. char c;
  89. int ws=0;    /* white spaces */
  90.  
  91.     if (s[0] == 0x2a){
  92.         t[0]=0;
  93.         return(0);
  94.     }
  95.     while (s[i] != 0){
  96.         c=s[i];
  97.         if (c == 0x3b){
  98.             t[j]=0;
  99.             return(0);
  100.         }
  101.         if (c <= 0x20) {ws=1;i++;}
  102.         else {
  103.             if (ws != 0) {ws=0;t[j]=0x20;j++;}
  104.             t[j]=s[i];
  105.             i++;j++;
  106.         }
  107.     }
  108.     t[j]=0;
  109. }
  110.